package me.skean.synologyphotosutil.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Tag;
import me.skean.synologyphotosutil.OperationService;
import me.skean.synologyphotosutil.bean.FilesRes;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "文件管理")
@RestController
@RequestMapping("/files")
@ApiSupport(order = 10)
public class FilesController {
    @Resource
    private OperationService service;

    @ApiOperationSupport(order = 11)
    @PostMapping(value = "/listFiles", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "列出文件", notes = "列出指定目录下的文件")
    public ResponseEntity<FilesRes> listFiles(
            @ApiParam(value = "目录路径")
            @RequestParam(value = "path", required = false)
            String path,

            @ApiParam(value = "关键字")
            @RequestParam(value = "key", required = false)
            String key) {
        return ResponseEntity.ok(service.fileList(path, key));
    }

    @ApiOperationSupport(order = 12)
    @PostMapping(value = "/testRename", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "模拟重命名", notes = "模拟文件重命名操作，不实际执行")
    public ResponseEntity<FilesRes> testRename(
            @ApiParam(value = "目录路径")
            @RequestParam(value = "path", required = false)
            String path,
            @ApiParam(value = "目标字符, 可以多个逗号分割, 支持正则表达式")
            @RequestParam(value = "target", required = false)
            String target,
            @ApiParam(value = "替换字符, 可以多个逗号分割, 要跟目标字符数量匹配")
            @RequestParam(value = "replacement", required = false)
            String replacement) {
        return ResponseEntity.ok(service.fileOperation(path, target, replacement, false));
    }

    @ApiOperationSupport(order = 13)
    @PostMapping(value = "/doRename", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "重命名", notes = "实际执行文件重命名操作")
    public ResponseEntity<FilesRes> doRename(
            @ApiParam(value = "目录路径")
            @RequestParam(value = "path", required = false)
            String path,
            @ApiParam(value = "目标字符, 可以多个逗号分割, 支持正则表达式")
            @RequestParam(value = "target", required = false)
            String target,
            @ApiParam(value = "替换字符, 可以多个逗号分割, 要跟目标字符数量匹配")
            @RequestParam(value = "replacement", required = false)
            String replacement) {
        return ResponseEntity.ok(service.fileOperation(path, target, replacement, true));
    }

}
